package org.example.panel.coursepanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class SearchCoursePanel extends JPanel {
    public SearchCoursePanel(){
        //组件准备
        JTextField course = new JTextField(15);
        JButton searchCourse = new JButton("查找课程");
        Box courseBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        JTextArea textArea = new JTextArea(15,10);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(300,230));
        //注册监视器
        searchCourse.addActionListener((ActionEvent e) -> {
            if(course.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "课程名不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else{
                String string = CourseOperation.searchCourse(course.getText());
                if(string.isEmpty()){
                    JOptionPane.showMessageDialog(null,
                            "找不到课程", null, JOptionPane.WARNING_MESSAGE);
                }else{
                    textArea.setText(string);
                }
            }
        });
        //添加组件
        courseBox.add(new JLabel("课程名： "));
        courseBox.add(course);
        courseBox.add(Box.createHorizontalStrut(5));
        courseBox.add(searchCourse);
        box.add(courseBox);
        box.add(Box.createVerticalStrut(5));
        textArea.setEditable(false);
        box.add(scrollPane);
        add(box);
    }
}
